package net.demo.dal.user;

import java.io.Serializable;

/**
 * 用户等级
 */
public class UserLevel implements Serializable{
	
	private static final long serialVersionUID = 2671617083592439961L;
	
	public static final UserLevel LEVEL_COMMON =new UserLevel(0,"普通会员");
	public static final UserLevel LEVEL_VIP =new UserLevel(1,"VIP会员");
	public static final UserLevel LEVEL_AGENT =new UserLevel(2,"代理商");
	
	
	private static final UserLevel[] LEVELS ={ LEVEL_COMMON, LEVEL_VIP, LEVEL_AGENT };
	
	
	private final int intValue;
	private final String name;
	
	
	private UserLevel(int intValue,String name){
		this.intValue = intValue;
		this.name = name;
	}
	
	public String toString(){
		return name;
	}
	
	public int intValue(){
		return intValue;
	}
	
	
	public static UserLevel convertIntToLevel(int levelAsInt) throws IllegalArgumentException{
		if( levelAsInt < 0 || levelAsInt >= LEVELS.length ){
			throw new IllegalArgumentException("level value as value must between 0 and " + (LEVELS.length-1 ) );
		}
		return LEVELS[levelAsInt];
	}
	
	
	public int hashCode(){
		return intValue;
	}
	
	
	public boolean equals(Object object){
		if( !(object instanceof UserLevel) ) {
			return false;
		}
		return ( (UserLevel) object ).intValue == this.intValue;
	}
	
}
